//
//  LDYstatusPictureView.swift
//  李东阳微博
//
//  Created by 李东阳 on 16/4/21.
//  Copyright © 2016年 李东阳. All rights reserved.
//

import UIKit

//cell的重用标识
private let StatusPictureViewCellId = "StatusPictureViewCellId"
// cell 间距
private let itemMargin: CGFloat = 5
// cell 的宽高
private let cellWH: CGFloat = (UIScreeonwidth - 20 - itemMargin * 2) / 3

class LDYstatusPictureView: UICollectionView {

    
    var picInfo:[LDYPicInfo]?{
    
        didSet{
            

        
            let size = getPictureViewSize(picInfo?.count ?? 0)
            
            self.snp_updateConstraints { (make) -> Void in
                
                make.size.equalTo(size)
            }
       
      "TODO:|FIXME: 得到数据记得刷新???"
            self.reloadData()
            
        }
}
    
        //MARK: -  处理配图的size
    
    func getPictureViewSize(count: Int) -> CGSize{
       
        if count <= 0 {
        
        return CGSizeZero
        }
        
            //MARK: -  行数 和列数
        let col = count>=3 ? 3 : count
        let row = (count - 1) / 3 + 1
        
//      配图的宽高
        let width = CGFloat(col) * cellWH + CGFloat(col - 1) * itemMargin
        let height = CGFloat(row) * cellWH + CGFloat(row - 1) * itemMargin
        return CGSize(width: width, height: height)
    }
    

    
  override  init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {

   super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
    
    setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
        //MARK: -  设置视图

    private   func setupUI(){
    
        self.backgroundColor = UIColor.clearColor()
        
       addSubview(contLabel)
        
       dataSource = self
            //MARK: -  注册cell
        registerClass(LDYstatusPicureCell.self, forCellWithReuseIdentifier: StatusPictureViewCellId)

        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
            //MARK: -  设置元素大小
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        
            //MARK: -  设置垂直和水平间距
       layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        
        
        
      contLabel.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(self)
        }
    
    }
    
    private lazy var contLabel: UILabel = UILabel(textColor: UIColor.redColor(), textFont: 34)
}



extension LDYstatusPictureView:UICollectionViewDataSource{
    
//    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
//    
//      
//        
//    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
     
        return picInfo?.count ?? 0
    }


    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
      
        
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier(StatusPictureViewCellId, forIndexPath: indexPath) as!LDYstatusPicureCell
//        cell.backgroundColor = randomColor()
        cell.pic = picInfo![indexPath.item]
        
        return cell
    }
    
}

class LDYstatusPicureCell: UICollectionViewCell {
    
    var pic:LDYPicInfo?{
        didSet{
        
        iconImageView.sd_setImageWithURL(NSURL(string: pic?.thumbnail_pic ?? ""), placeholderImage:  UIImage(named: "timeline_card_top_background"))
        
            // 动态图片
            if let p = pic?.thumbnail_pic where p.hasSuffix(".gif") {
                gifImageView.hidden = false
            }else{
                // 不是动态图片
                gifImageView.hidden = true
            }
            
        }
    
    }
    
    
   override  init(frame: CGRect) {
        super.init(frame: frame)
    
    setupUI()
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
  
   private func setupUI(){
    
    self.backgroundColor = randomColor()
   contentView.addSubview(iconImageView)
    contentView.addSubview(gifImageView)
    
        //MARK: -  添加约束 01
    iconImageView.snp_makeConstraints { (make) -> Void in
        
        make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
    }
    
   gifImageView.snp_makeConstraints { (make) -> Void in
        make.trailing.bottom.equalTo(contentView)
    }
    
    }
    
        //MARK: -  懒加载控件
    private lazy var iconImageView:UIImageView = {
    let img =  UIImageView(image: UIImage(named: "timeline_card_top_background"))
        
        img.contentMode = .ScaleAspectFill
        img.clipsToBounds = true
        return img
    }()
   
        //MARK: -  动态图片logo
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    

}





